package com.haotian.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**

<filter>
	<filter-name>encodingFilter</filter-name>
		<filter-class>com.haotian.web.filter.CharacterEncodingFilter</filter-class>
		<async-supported>true</async-supported>
	<init-param>
		<param-name>encoding</param-name>
		<param-value>UTF-8</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>encodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>


**/
/**
 * @author 张昊天 2017年12月7日
 * 
 *         继承本类 编写注解或配置web.xml
 * 
 * @WebFilter(urlPatterns = "/*",initParams = { @WebInitParam(name = "encoding",
 *                        value = "utf-8") })
 */

public abstract class _WebCharacterEncodingFilter implements Filter {

	String encoding;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		encoding = filterConfig.getInitParameter("encoding"); // 获取初始化参数
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		if (encoding != null) {
			request.setCharacterEncoding(encoding); // 设置request的编码格式
			response.setCharacterEncoding(encoding); // 设置response字符编码
		}
		chain.doFilter(request, response); // 传递给下一个过滤器
	}

	@Override
	public void destroy() {

	}
}
